package com.yugao.fintech.antelope.shortlink.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.base.security.utils.SecurityUtils;
import com.yugao.fintech.antelope.shortlink.assembly.LinkGroupAssembly;
import com.yugao.fintech.antelope.shortlink.manager.SlLinkGroupManager;
import com.yugao.fintech.antelope.shortlink.mapper.SlLinkGroupMapper;
import com.yugao.fintech.antelope.shortlink.mapper.SlShortLinkManager;
import com.yugao.fintech.antelope.shortlink.model.entity.LinkGroup;
import com.yugao.fintech.antelope.shortlink.model.entity.ShortLink;
import com.yugao.fintech.antelope.shortlink.model.rqrs.LinkGroupListReq;
import com.yugao.fintech.antelope.shortlink.model.rqrs.LinkGroupPageReq;
import com.yugao.fintech.antelope.shortlink.model.rqrs.LinkGroupReq;
import com.yugao.fintech.antelope.shortlink.service.LinkGroupService;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import com.yugao.fintech.framework.mybatisplus.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 短链组
 */
@Slf4j
@Service
public class LinkGroupServiceImpl extends ServiceImpl<SlLinkGroupMapper, LinkGroup> implements LinkGroupService {
    @Autowired
    private SlLinkGroupManager slLinkGroupManager;
    @Autowired
    private LinkGroupAssembly linkGroupAssembly;
    @Autowired
    private SlShortLinkManager linkGroupManagerManager;

    /**
     * 分页查询 短链组
     *
     * @return 返回分页数据
     */
    @Override
    public IPage<LinkGroup> pageGroup(LinkGroupPageReq req) {
        LambdaQueryWrapper<LinkGroup> queryWrapper = LinkGroup.lqw()
                .like(StringUtils.isNotEmpty(req.getName()), LinkGroup::getName, req.getName());
        return this.page(PageUtil.to(req), queryWrapper);
    }

    /**
     * 保存 短链组
     *
     * @param req 新增的记录
     * @return 返回影响行数
     */
    @Override
    public boolean saveGroup(LinkGroupReq rq) {
        LinkGroup entity = linkGroupAssembly.toLinkGroup(rq);
        entity.setId(null);
        entity.setUserId(SecurityUtils.getUserId());
        return slLinkGroupManager.saveGroup(entity);
    }


    /**
     * 更新 短链组
     *
     * @param rq 修改的记录
     * @return 返回影响行数
     */
    @Override
    public boolean updateGroup(LinkGroupReq rq) {
        LinkGroup entity = linkGroupAssembly.toLinkGroup(rq);
        entity.setUserId(SecurityUtils.getUserId());
        return slLinkGroupManager.updateByIdAndAccount(entity);
    }

    /**
     * 通过id删除 短链组
     *
     * @param id 删除的主键
     * @return 返回影响行数
     */
    @Override
    public boolean deleteGroupById(Long id) {
        if (linkGroupManagerManager.count(ShortLink.lqw().eq(ShortLink::getGroupId, id)) > 0) {
            throw new BizException("已有短链引用当前组");
        }
        LambdaQueryWrapper<LinkGroup> wrapper = LinkGroup.lqw()
                .eq(LinkGroup::getUserId, SecurityUtils.getUserId()).eq(LinkGroup::getId, id);
        return this.remove(wrapper);
    }

    /**
     * 通过ids删除 短链组
     *
     * @param ids 删除的主键
     * @return 返回影响行数
     */
    @Override
    public boolean deleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return true;
        }
        LambdaQueryWrapper<LinkGroup> wrapper = LinkGroup.lqw()
                .eq(LinkGroup::getUserId, SecurityUtils.getUserId()).in(LinkGroup::getId, ids);
        return this.remove(wrapper);
    }

    @Override
    public List<LinkGroup> listGroup(LinkGroupListReq req) {
        LambdaQueryWrapper<LinkGroup> queryWrapper = LinkGroup.lqw()
                .like(StringUtils.isNotEmpty(req.getName()), LinkGroup::getName, req.getName());
        return this.list(queryWrapper);
    }
}
